::version 20220420
@echo off
setlocal ENABLEDELAYEDEXPANSION

rem 目录设置
pushd %cd%
chcp 65001
cd  %~dp0
set QTDIR=%Qt5_15%
set CURDIR=%cd%
set APPNAME="UPCNC3.exe"
set CmdTitle=WINDOWS UPCNC v3 自动编译脚本
echo --------------------------------------------------------------

echo %CmdTitle%
echo.
echo.

rem tools
set CURDIR=%cd%
if not defined BUILDTOOL (
    set BUILDTOOL=%CURDIR%\..\..\buildtool
)
set SVNTOOL=%BUILDTOOL%\Git\bin\git.exe
set SEDTOOL=%BUILDTOOL%\Git\usr\bin\sed.exe
set MVTOOL=%BUILDTOOL%\Git\usr\bin\mv.exe
set SYMPATH=%BUILDTOOL%\symstore.exe
set LAN_CONVERT=%BUILDTOOL%\excel2ts\csv2ts.js
set CLEAN=%CURDIR%\clean.bat
rem 工程环境设置
set BUILDENVBAT="%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat"
set PROJECTPATH=%CURDIR%\..
set SLNNAME="%PROJECTPATH%\WinUPCNC.sln"
set BRANCHNAME=master
if not "%~1"=="" (  
  set BRANCHNAME=%~1
)
title %CmdTitle%-branch-%BRANCHNAME%
rem 存放目录设置
set BIN_RELEASE=%PROJECTPATH%\Bin\x64\Release
set PACKAGE_PATH=%PROJECTPATH%\output\Package
set PDB_PATH=%PROJECTPATH%\output\Pdb
set SETUP_TS=%CURDIR%\..\res\views\wints

rem 标志位
set BUILD_SUCCESS=true
set IS_BUILD=false
set LAST_TIME=UNKNOW
set LAST_VER=UNKNOW
rem ****debug****

rem *****************************************************************
echo 正在清理文件夹...
call :CLEAN_DIR

if defined CI_COMMIT_BRANCH (
::CI环境才进行强制源码更新
echo 正在更新git...
call :SVN_UPDATE
)

call %BUILDENVBAT%
echo 正在比较svn版本号...
call :CHECK_SVNVER
if %IS_BUILD% EQU false (
echo 未检测到更改, 正在跳过编译
goto :BUILDEND
)

echo 正在生成 ts 文件...
call :csvTots

echo 正在编译...
call :BUILD_SLN
if %BUILD_SUCCESS% NEQ true (
echo 编译失败....正在取消
goto :ENDERROR
)
echo %NEWSVNVER% > svnver.dat

:BUILDEND

set LAST_VER=%NEWSVNVER%

%SVNTOOL% log -1 --format=%%ad --date=format:%%Y.%%m%%d.%%H%%M.0 > x
for /f "tokens=1-4 delims= " %%1 in (x) do set LAST_TIME=%%1
del /Q x

echo 正在拷贝文件...
call :COPY_PACKAGE

echo 正在查找依赖...
call :SEARCH_DEPLOYQT
goto :END


:SEARCH_DEPLOYQT
%QTDIR%\bin\windeployqt.exe --release --no-translations -websockets -plugindir %PACKAGE_PATH% --qmldir %PACKAGE_PATH%\views\ %PACKAGE_PATH%\UPCNC3.exe
copy %QTDIR%\bin\Qt5Sql.dll %PACKAGE_PATH%\
mkdir %PACKAGE_PATH%\sqldrivers
copy %QTDIR%\plugins\sqldrivers\qsqlite.dll %PACKAGE_PATH%\sqldrivers\
echo 查找依赖完成...
goto :eof
rem *****************************************************************

:CLEAN_DIR
if exist %PACKAGE_PATH% (rmdir /s /q %PACKAGE_PATH%)
if exist %PDB_PATH% (rmdir /s /q %PDB_PATH%)
goto :eof

:SVN_UPDATE
rem 强制用远程代码覆盖更新本地文件
%SVNTOOL% fetch  --all
%SVNTOOL% reset  --hard origin/%BRANCHNAME%
%SVNTOOL% pull
%SVNTOOL% submodule update --init --recursive
goto :eof

:BUILD_SLN
if exist %BIN_RELEASE% (rmdir /s /q %BIN_RELEASE%)
devenv %SLNNAME% /rebuild release

if NOT %ERRORLEVEL% == 0 (
set BUILD_SUCCESS=false
exut /b %ERRORLEVEL%
)

goto :eof

:CHECK_SVNVER
rem 获取新版本号
set strtemp=svnver
%SVNTOOL% rev-list HEAD --abbrev-commit --max-count=1 > %strtemp%
for /f "tokens=1-4 delims= " %%1 in (%strtemp%) do set NEWSVNVER=%%1
del /Q %strtemp%

rem 读取旧版本号
if exist svnver.dat (
set /P OLDSVNVER=<svnver.dat
) else (
set OLDSVNVER=0
)



if %NEWSVNVER% NEQ %OLDSVNVER% (
set IS_BUILD=true
) else (
set IS_BUILD=false
)

echo newver: %NEWSVNVER%
echo oldver: %OLDSVNVER%
echo build : %IS_BUILD%
goto :eof

:COPY_PACKAGE
mkdir %PACKAGE_PATH%
mkdir %PDB_PATH%
xcopy %BIN_RELEASE% %PACKAGE_PATH% /E /F /R /Y
copy %PACKAGE_PATH%\*.pdb %PDB_PATH% /Y
del /Q %PACKAGE_PATH%\*.pdb
call %CLEAN%
xcopy %CURDIR%\..\res\conf\* %PACKAGE_PATH%\ /y /s
del /Q %PACKAGE_PATH%\UPCNC3-*.ini
del /Q %PACKAGE_PATH%\sys-*.json
xcopy %CURDIR%\..\res\script\ %PACKAGE_PATH%\script\ /y /s /R
xcopy %CURDIR%\..\res\third\* %PACKAGE_PATH%\ /y /s
xcopy %CURDIR%\..\res\views\ %PACKAGE_PATH%\views\ /y /s /R
rd /S /Q %PACKAGE_PATH%\views\linux %PACKAGE_PATH%\views\linuxts %PACKAGE_PATH%\views\linuximg %PACKAGE_PATH%\views\base2 %PACKAGE_PATH%\views\P55W %PACKAGE_PATH%\views\P55Wimg %PACKAGE_PATH%\views\P55Wts
del /Q %PACKAGE_PATH%\views\linux*
del /Q %PACKAGE_PATH%\views\*.qrc
del /Q %PACKAGE_PATH%\views\wints\*.*
xcopy %CURDIR%\..\res\views\wints\*.qm %PACKAGE_PATH%\views\wints\ /y /s /R
xcopy %QTDIR%\qml\Qt\labs\platform\qtlabsplatformplugin.dll %PACKAGE_PATH%\Qt\labs\platform\ /Y /F /E
xcopy %QTDIR%\qml\Qt\labs\platform\plugins.qmltypes %PACKAGE_PATH%\Qt\labs\platform\ /Y /F /E
xcopy %QTDIR%\qml\Qt\labs\platform\qmldir %PACKAGE_PATH%\Qt\labs\platform\ /Y /F /E

rem 修改版本号
set VERSION_FILE=%CURDIR%\..\output\Package\sys.json
%SEDTOOL% -e "s/{time}/%LAST_TIME%/g" %VERSION_FILE% > %VERSION_FILE%.tmp
%MVTOOL% %VERSION_FILE%.tmp %VERSION_FILE% -f
goto :eof

:csvTots
node %LAN_CONVERT% "%SETUP_TS%\P54DTrans.csv" "%SETUP_TS%"
goto :eof

:ENDERROR
echo %CmdTitle%错误，请检查log
exit /b 1

:END
popd